package com.luoran.car.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luoran.car.config.UploadMinioUtils2;
import com.luoran.car.domain.CarCity;
import com.luoran.car.domain.City;
import com.luoran.car.domain.Syscustomer;
import com.luoran.car.domain.Vehicle;
import com.luoran.car.service.CarCityService;
import com.luoran.car.service.SyscustomerService;
import com.luoran.car.service.VehicleService;
import com.luoran.car.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("Customer")
public class SysCustomerController {

    @Autowired
    SyscustomerService syscustomerService;

    @Autowired
    VehicleService vehicleService;

    @Autowired
    CarCityService cityService;

    //查询，列表分页
    @RequestMapping("SysCustomerList")
    public Result SysCustomerList(@RequestBody Syscustomer syscustomer, @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Syscustomer> list = syscustomerService.customerList(syscustomer);
        PageInfo<Syscustomer> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    //添加
    @RequestMapping("SysCustomerAdd")
    public Result SysCustomerAdd(@RequestBody Syscustomer syscustomer){
        syscustomer.setCreateTime(new Date());
        boolean save = syscustomerService.save(syscustomer);
        return save?Result.success():Result.fail();
    }
    //修改
    @RequestMapping("SysCustomerUpdate")
    public Result SysCustomerUpdate(@RequestBody Syscustomer syscustomer){
        boolean b = syscustomerService.updateById(syscustomer);
        return b?Result.success():Result.fail();
    }
    //删除
    @RequestMapping("SysCustomerDelete")
    public Result SysCustomerDelete(Integer customerId){
        boolean b = syscustomerService.removeById(customerId);
        return b?Result.success():Result.fail();
    }

    //导出excel表格
    @RequestMapping("excelSysCustomer")
    public void excelSysCustomer(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("客户表", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreansheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<Syscustomer> list = syscustomerService.list();
        EasyExcel.write(response.getOutputStream(),Syscustomer.class).sheet("客户信息").doWrite(list);
    }

    @RequestMapping("getList")
    public Result getList(){
        List<Vehicle> list = vehicleService.getList();
        return Result.success(list);
    }

    @RequestMapping("importCustomer")
    public Map<String,Object> importCustomer(MultipartFile file){
        HashMap<String, Object> hashMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(),Syscustomer.class,new CellDataDemoHeadDataListenerCustomer(syscustomerService)).sheet().doRead();
            hashMap.put("msg","成功");
        }catch (IOException e){
            e.printStackTrace();;
            hashMap.put("msg","失败");
        }
        return hashMap;
    }

    @RequestMapping("getCity")
    public Result getCity(){
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<CarCity> list = cityService.getList();
        List<Tree<Object>> treeNodes = TreeUtil.<CarCity, Object>build(list, 1, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCityId());
                    tree.setParentId(treeNode.getPid());
                    tree.setName(treeNode.getCityName());
                });
        return Result.success(treeNodes);
    }

    @RequestMapping("customerZxt")
    public Result customerZxt(){
        List<Map<String,String>> list = syscustomerService.customerZxt();
        return Result.success(list);
    }

    @RequestMapping("upload")
    public String upload(MultipartFile file){
        String s = UploadMinioUtils2.uploadPic(file);
        return s;
    }

    @RequestMapping("getSearch")
    public Result getSearch(Integer customerId){
       Syscustomer syscustomer = syscustomerService.getSearch(customerId);
       return Result.success(syscustomer);
    }
}
